﻿#include <QApplication>
#include <QWidget>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QLabel>
#include <QTextEdit>
#include <QPainter>
#include <QPainterPath>

class BubbleWidget : public QWidget {
public:
    explicit BubbleWidget(bool isSender, QWidget* parent = nullptr)
        : QWidget(parent), m_isSender(isSender) {
        setContentsMargins(0, 0, 0, 0);

        // 使用 QLabel 代替 QTextEdit
        m_label = new QLabel(this);
        m_label->setWordWrap(true); // 允许文本自动换行
        m_label->setTextFormat(Qt::PlainText); // 纯文本模式
        m_label->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
        m_label->setContentsMargins(10, 5, 10, 5); // 设置文本边距

        QHBoxLayout* layout = new QHBoxLayout(this);
        layout->setContentsMargins(0, 0, 0, 0); // 消除布局边距
        layout->addWidget(m_label);
    }

    void setText(const QString& text) {
        m_label->setText(text);
        adjustSize(); // 调整气泡大小以适应文本
    }

protected:
    void paintEvent(QPaintEvent* event) override {
        QPainter painter(this);
        painter.setRenderHint(QPainter::Antialiasing);

        QColor bubbleColor = m_isSender ? QColor("#98E165") : QColor("#FFFFFF");

        // 绘制气泡主体
        QRect rect = this->rect().adjusted(0, 0, -10, 0);
        QPainterPath path;
        path.addRoundedRect(rect, 5, 5);

        // 绘制箭头 箭头不会对齐头像，一般不显示昵称以显示箭头
       /* if (m_isSender) {
            path.moveTo(rect.width(), rect.height() / 2 - 5);
            path.lineTo(rect.width() + 10, rect.height() / 2) ;
            path.lineTo(rect.width(), rect.height() / 2 + 5);
        }
        else {
            path.moveTo(0, rect.height() / 2 - 5);
            path.lineTo(-10, rect.height() / 2);
            path.lineTo(0, rect.height() / 2 + 5);
        }*/

        painter.fillPath(path, bubbleColor);
        QWidget::paintEvent(event);
    }

    QSize sizeHint() const override {
        // 根据文本内容动态计算气泡大小
        m_label->setFont(QFont("黑体", 11));
        return m_label->sizeHint() + QSize(20, 10); // 增加一些边距
    }

private:
    bool m_isSender;
    QLabel* m_label; // 使用 QLabel 显示文本
};

class ChatMessageWidget : public QWidget {
public:
    ChatMessageWidget(const QPixmap& avatar, const QString& username,
        const QString& message, bool isSender, QWidget* parent = nullptr)
        : QWidget(parent), m_isSender(isSender) {

        // 头像
        QLabel* avatarLabel = new QLabel;
        avatarLabel->setPixmap(avatar.scaled(40, 40, Qt::KeepAspectRatio));
        avatarLabel->setFixedSize(40, 40);

        // 昵称
        QLabel* nameLabel = new QLabel(username);
        nameLabel->setStyleSheet("color: #666666; font-size: 12px;");

        // 气泡
        BubbleWidget* bubble = new BubbleWidget(m_isSender);
        bubble->setText(message);

        // 布局
        QVBoxLayout* msgLayout = new QVBoxLayout;

        // 自己发送昵称,不显示
        // 不是自己的情况
        if (!m_isSender) {
            msgLayout->addSpacing(1);
            msgLayout->addWidget(nameLabel, 0, m_isSender ? Qt::AlignRight : Qt::AlignLeft);
        }
        else
            msgLayout->addSpacing(17);

        msgLayout->addWidget(bubble);
        QHBoxLayout* mainLayout = new QHBoxLayout(this);
        mainLayout->setContentsMargins(5, 5, 5, 5);
        mainLayout->setSpacing(5);

        if (m_isSender) {
            mainLayout->addStretch(0);
            mainLayout->addLayout(msgLayout);
            mainLayout->addWidget(avatarLabel);
        }
        else {
            mainLayout->addWidget(avatarLabel);
            mainLayout->addLayout(msgLayout);
            mainLayout->addStretch();
        }
    }

private:
    bool m_isSender;
};
